<template>
  <ContentWrap :title="title" v-loading="formLoading">
    <nChooseNActivityForm ref="activityRef" :propFormData="formData" />
    <el-form>
      <el-form-item style="float: right">
        <el-button :loading="formLoading" type="primary" @click="submitForm"> 保存 </el-button>
        <el-button @click="close">返回</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>
<script lang="ts" setup>
import { cloneDeep } from 'lodash-es'
import nChooseNActivityForm from './nChooseNActivityActivityForm.vue' //
import { useTagsViewStore } from '@/store/modules/tagsView'
import * as NChooseNActivityApi from '@/api/mall/promotion/nChooseN/nChooseNActivity'

defineOptions({ name: 'TenantAdd' })
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const activityRef = ref() // 商品信息 Ref
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const title = ref('N选N活动创建')
const { push, currentRoute } = useRouter() // 路由
const { params } = useRoute() // 查询参数
const { delView } = useTagsViewStore() // 视图操作
const formData = ref({
  name: undefined,
  createTime: undefined,
  startAndEndTime: [] as any,
  remark: undefined,
  salesChannel: [], //参与渠道
  price: undefined, //活动规则
  number: undefined, // 活动数量
  discountScopes: [], //抵扣设置
  authorizedTenant: [], //关联门店
  salesOnlineChannel: [], //线上渠道
  products: [],

  isUpdated: false,
  startTime: undefined,
  endTime: undefined
})

/** 获得详情 */
const getDetail = async () => {
  const id = params.id as unknown as number
  formType.value = 'create'

  if (id) {
    formLoading.value = true
    formType.value = 'update'
    title.value = 'N选N活动编辑'
    try {
      formData.value = await NChooseNActivityApi.getNChooseNActivity(id)
      // 转区段时间
      formData.value.startAndEndTime = [formData.value.startTime, formData.value.endTime]
    } finally {
      formLoading.value = false
    }
  }
}
/** 提交按钮 */
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    // 校验各表单
    await unref(activityRef)?.validate()
    console.log(formData.value)

    // 深拷贝一份, 这样最终 server 端不满足，不需要影响原始数据
    const deepCopyFormData = cloneDeep(unref(formData.value))
    console.log(deepCopyFormData)
    // 时间段转换
    deepCopyFormData.startTime = deepCopyFormData.startAndEndTime![0]
    deepCopyFormData.endTime = deepCopyFormData.startAndEndTime![1]
    delete deepCopyFormData.startAndEndTime
    // 校验都通过后提交表单
    const data = deepCopyFormData as unknown as NChooseNActivityApi.NChooseNActivityVO
    const id = params.id as unknown as number
    if (!id) {
      await NChooseNActivityApi.createNChooseNActivity(data)

      message.success(t('common.createSuccess'))
    } else {
      await NChooseNActivityApi.updateNChooseNActivity(data)

      message.success(t('common.updateSuccess'))
    }
    close()
  } finally {
    formLoading.value = false
  }
}

/** 关闭按钮 */
const close = () => {
  delView(unref(currentRoute))
  push({ name: 'nChooseNActivity' })
}
/** 初始化 */
onMounted(async () => {
  await getDetail()
})
</script>
